<?php
 
class RegisterAPI {
    function didRegister() {
    	$variable = file_get_contents('https://choku-shoku.firebaseio.com/registered.json');
		$decoded = json_decode($variable);
		$json = json_decode(file_get_contents("php://input"));
		$email = $json->email;
		$guid = $json->guid;
		$id = $json->id;
		//echo $email;
        foreach($decoded as $val) {
    		if($val->email == $email && $val->guid == $guid && $val->id == $id){
    			//echo "we've got a match";
				//echo "<br/>";
				
    			$mandrill_key = 'tHhV7OsYFKcrpswvLd07Jg';
				$mandrill_base_url = 'https://mandrillapp.com/api/1.0';
				$mandrill_template_send = '/messages/send-template.json';
				$mandrill_template_name = 'Registered';
				
				$mandrill_header_content = 'Your business name is ' . $val->businessName . '.';
				$mandrill_main_content = 'your id is' . $val->id . 'confirm your account at http://localhost/ChokuShoku/test.html#/confirmRegistration?email=' . $val->email . '&id=' . $val->id . '&guid=' . $val->guid;
				$mandrill_footer_content = '';
				
				$mandrill_template_content = array(
											array(
											'name' => 'header',
											'main' => $mandrill_main_content,
											'content' => $mandrill_header_content),
											array(
											'name' => 'main',
											'main' => $mandrill_main_content,
											'content' => $mandrill_main_content),
											array(
											'name' => 'footer',
											'main' => $mandrill_main_content,
											'content' => $mandrill_footer_content));
											
										
				$mandrill_email_subject = 'Please confirm your account ' . $val->email;
				$mandrill_from_email = 'ChokuShoku@ChokuShoku.com';
				$mandrill_from_name = 'ChokuShoku';
				$mandrill_recipient_name = $val->businessName;
				$mandrill_recipient_email = $val->email;
				$mandrill_recipients = array(array('email'=>$mandrill_recipient_email, 'name'=>$mandrill_recipient_name, 'type'=>'to'));
				$mandrill_headers = array('Reply-To'=>'chokuShoku@chokushoku.com');
										
				$mandrill_message_content = array('subject'=>$mandrill_email_subject, 'from_email'=>$mandrill_from_email, 'from_name'=>$mandrill_from_name, 'to'=>$mandrill_recipients, 'headers'=>$mandrill_headers);											
				
    			$mandrill_email_data = array(
    							'key' => $mandrill_key,
							    'template_name' => $mandrill_template_name,
							    'template_content' => $mandrill_template_content,
							    'message'=>$mandrill_message_content);
				$mandrill_post_data = json_encode($mandrill_email_data);
				
				//var_dump($mandrill_post_data);
				
				//post to mandrill
				$url = $mandrill_base_url . '' . $mandrill_template_send;

				$ch = curl_init($url);
				 
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $mandrill_post_data);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				 
				$response = curl_exec($ch);
				//echo $response;
				curl_close($ch);
				//header('Content-Type: application/json');
				echo json_encode(array('status' => 'success'));
				return;
    		}else{
    			//do nothing
    			//echo "not a match";
				//echo "<br/>";
				//header('Content-Type: application/json');
    		}
		}
		echo json_encode(array('status' => 'failure', reason => 'unkown'));
    }
}
 

$api = new RegisterAPI;
$api->didRegister();
 
?>